package com.jstyle.jclife.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.jstyle.jclife.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 打卡
 * Created by Administrator on 2018/8/30.
 */

public class CheckInAdapter extends RecyclerViewBaseAdapter<String> {


    public CheckInAdapter(List<String> mList) {
        super(mList);
    }
    public CheckInAdapter(List<String> mList,List<Integer>list) {
        super(mList);
        this.checkedList=list;
    }

    @Override
    protected void bindData(BaseViewHolder holder, int position) {

    }

    @Override
    protected int getLayoutId(int viewType) {
        return R.layout.item_clock_mode;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ClockModeViewHolder(getInflaterView(parent, viewType));
    }
    List<Integer>checkedList=new ArrayList<>();
    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
        ClockModeViewHolder clockModeViewHolder=(ClockModeViewHolder)holder;
        clockModeViewHolder.btClockModeName.setText(mDataList.get(position));
        clockModeViewHolder.btClockModeName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkedList.contains(position)){
                    checkedList.remove(checkedList.indexOf(position));
                }else{
                    checkedList.add(position);
                }
                notifyDataSetChanged();
            }
        });
        setDrawable(clockModeViewHolder,position);
        setImageResource(clockModeViewHolder,position);
    }
    private void setDrawable(ClockModeViewHolder clockModeViewHolder,int position){
        int id=R.drawable.clock_mode_checked;
        Context context=clockModeViewHolder.itemView.getContext();
        Drawable drawable = context.getResources().getDrawable(id);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        clockModeViewHolder.btClockModeName.setCompoundDrawables(null, null, checkedList.contains(position)?drawable:null, null);

    }
    private void setImageResource(ClockModeViewHolder clockModeViewHolder,int position){
        int id=0;
        switch (position){
            case 0:
                id=R.drawable.clock_mode_step;
                break;
            case 1:
                id=R.drawable.clock_mode_sport;
                break;
            case 2:
                id=R.drawable.clock_mode_sleep;
                break;
            case 3:
                id=R.drawable.clock_mode_heart;
                break;
        }
        clockModeViewHolder.ivClockMode.setImageResource(id);
    }
    public class ClockModeViewHolder extends BaseViewHolder {
        @BindView(R.id.iv_clockMode)
        ImageView ivClockMode;
        @BindView(R.id.bt_clock_modeName)
        Button btClockModeName;
        public ClockModeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public List<Integer>getCheckedList(){
        return this.checkedList;
    }
}
